package cn.socket.chat;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 聊天程序案列 客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //tcp客户端
        Socket s=new Socket("192.168.59.1",10000);
        System.out.println("--------------------客户端启动-----------------------------");
        //数据键盘读取
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        InputStream in = s.getInputStream();
        String line;
        //不是bye就一直执行 同时从键盘读且给line
        while ((line=br.readLine())!=null){
          if (line.equals("bye")){
              break;
          }
        //客户端发送数据
        bw.write(line);
        //换行
        bw.newLine();
        //刷新数据
        bw.flush();
        //客户端接收数据
         byte[] data=new byte[1024];
         in.read(data);
         String msg=new String(data,0, data.length);
         System.out.print("服务端:");
         System.out.println(msg);
        }
        //释放资源
        s.close();
    }
}
